
<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="请输入要吐槽的内容 别tm多bb" maxlength="120" v-model="content"></textarea>

        <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>

        <div class="cmt-list">
            <div class="cmt-item" v-for="(item,i) in comments" :key="item.add_time">
                <div class="cmt-title">
                    第{{ i+1 }}楼 &nbsp;&nbsp;&nbsp;用户：{{ item.user_name }}&nbsp;发表时间：{{ item.add_time }}
                </div>
                <div class="cmt-body">
                    {{ item.content === 'undefined'?'此用户很懒，什么都么说也':item.content }}
                </div>
            </div>
        </div>

        <mt-button type="primary" size="large" plain>加载更多</mt-button>
    </div>
</template>    
<script>
import {Toast} from "mint-ui"
export default{
    data(){
        return {
            content:"",
            pageIndex:0,
            comments:[
                {user_name:"匿名用户",add_time:"2015-04-19T20:09:30.000Z",content:"沙发((`へ´*)ノ)"},
                {user_name:"匿名用户",add_time:"2015-04-19T20:09:30.000Z",content:"w(ﾟДﾟ)w"},
                {user_name:"匿名用户",add_time:"2015-04-19T20:09:30.000Z",content:"(ノへ￣、)"},
                {user_name:"匿名用户",add_time:"2015-04-19T20:09:30.000Z",content:"(￣_,￣ )"},
                {user_name:"匿名用户",add_time:"2015-04-19T20:09:30.000Z",content:"ヽ(✿ﾟ▽ﾟ)ノ"},
                {user_name:"匿名用户",add_time:"2015-04-19T20:09:30.000Z",content:"(๑•̀ㅂ•́)و✧"},
                {user_name:"匿名用户",add_time:"2015-04-19T20:09:30.000Z",content:"￣ε(#￣)☆╰╮o(￣皿￣///)"},
                {user_name:"匿名用户",add_time:"2015-04-19T20:09:30.000Z",content:"Σ( ° △ °|||)︴"},
                {user_name:"匿名用户",add_time:"2015-04-19T20:09:30.000Z",content:"φ(≧ω≦*)♪"},
                {user_name:"匿名用户",add_time:"2015-04-19T20:09:30.000Z",content:"︿(￣︶￣)︿"},
            ]
        }
    },
    created(){
        // 获取评论
        // this.getComments();
    },
    methods:{
        // 得到制定页数的评论
        getComments(){
            this.$http.get(url).then(result => {
                if (result.body.status === 0) {
                    // 数组拼接
                    this.comments = this.comments.concat(result.body.message);
                }else{
                    Toast("获取评论失败");
                }
            })
        },
        // 获取下一页的评论
        getMore(){
            this.pageIndex++;
            this.getComments();
        },
        // 提交用户 的评论
        postComment(){
            // 校验是否为空
            if (this.content.trim().length === 0) {
                Toast("评论不能为空");
                return;
            }
            // 提交参数 content = 评论内容
            this.$http.post(url,{content:this.content.trim()}).then(function(){
                if (result.body.status === 0) {
                    // 1.拼接处一个评论对象
                    var cmt = {user_name:"匿名用户",add_time:Date.now(),content:this.content.trim()};
                    // 在数组的第一位插入一个对象
                    this.comments.unshift(cmt);
                    this.content = "";
                }else{
                    Toast("评论提交失败");
                }
            })
        }
    },
    // 接受newsList传来的id  用于 发送get请求 获得comment
    props:["id"]
}
</script>
<style lang="less" scoped>
 .cmt-container{
     h3{
         font-size: 16px;
     }
     textarea {
         font-size: 14px;
         height: 85px; 
         margin: 0;
     }
     .cmt-list{
         margin: 5px 0px;
         .cmt-item{
            font-size: 13px;
            .cmt-title{
                line-height: 30px;
                background-color: #ccc;
            }
            .cmt-body{
                line-height: 30px;
                text-indent: 2em;
            }
         }
     }
 }
</style> 